package cn.tedu.store.service.impl;

import cn.tedu.store.entity.Product;
import cn.tedu.store.mapper.ProductMapper;
import cn.tedu.store.service.IProductService;
import cn.tedu.store.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    /**
     * 获取热销列表
     * @return
     */
    @Override
    public List<Product> getHotList() {
        //调用productMapper的findHotList方法进行查询
        List<Product> list = productMapper.findHotList();
        //对敏感数据，如显示优先级等置为null，不返回给控制器和前端
        for(Product product : list){
            product.setPriority(null);
        }
        return list;
    }

    /**
     * 获取指定id商品信息
     * @param id 商品id
     * @return
     */
    @Override
    public Product getProduct(Integer id) {
        Product product =  productMapper.findById(id);
        if (product==null){
            throw new ProductNotFoundException("商品未找到！");
        }
        //优先级敏感信息置空
        product.setPriority(null);
        return product;
    }
}
